package com.besall.allbase.view.activity.chipstoollevel4.protocol.FitnessParams;

import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_AUTO_PPG_DURATION;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_AUTO_PPG_ENDHOUR;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_AUTO_PPG_ENDMIN;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_AUTO_PPG_INTERVAL;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_AUTO_PPG_PPG;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_AUTO_PPG_STARTHOUR;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_AUTO_PPG_STARTMIN;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_ERASE_DATE_FITNESS;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_ERASE_DATE_VALUE;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;

public class AutoPPGInterval extends DialogFragment {
    public int intervals = 0;
    public int ppg = 0;
    int startHour = 0;
    int startMin = 0;
    int endHour = 0;
    int endMin = 0;
    int v = 0;
    private ConfigCallBack mConfigCallback;
    EditText mDuration;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.config_autoppginterval, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){

        RadioGroup chooseFormat = (RadioGroup) view.findViewById(R.id.ppg);
        chooseFormat.setOnCheckedChangeListener(FormatCheckChangeListener);
        mDuration = (EditText) view.findViewById(R.id.duration);
        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);
        intervals = 60;

        LoopView interval = (LoopView) view.findViewById(R.id.interval);
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            list.add("" + 30* (i + 2));
        }
        interval.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                intervals = 30 * (index + 2);
            }
        });
        interval.setItems(list);
        interval.setInitPosition(0);

        LoopView startH = (LoopView) view.findViewById(R.id.starth);
        ArrayList<String> hourList = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            hourList.add("" + i);
        }
        startH.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                startHour = index;
            }
        });
        startH.setItems(hourList);
        startH.setInitPosition(0);
        LoopView startM = (LoopView) view.findViewById(R.id.startm);
        ArrayList<String> mintList = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            mintList.add("" + i);
        }
        startM.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                startMin = index;
            }
        });
        startM.setItems(mintList);
        startM.setInitPosition(0);

        LoopView endH = (LoopView) view.findViewById(R.id.endh);
        ArrayList<String> endHourList = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            endHourList.add("" + i);
        }
        endH.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                endHour = index;
            }
        });
        endH.setItems(endHourList);
        endH.setInitPosition(0);
        LoopView endM = (LoopView) view.findViewById(R.id.endm);
        ArrayList<String> endMintList = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            endMintList.add("" + i);
        }
        endM.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                endMin = index;
            }
        });
        endM.setItems(endMintList);
        endM.setInitPosition(0);
    }

    public boolean saveConfig(){
        Log.i("AutoPPGInterval", "ppg: " + ppg );
        Log.i("AutoPPGInterval", "startHour: " + startHour );
        Log.i("AutoPPGInterval", "startMin: " + startMin );
        Log.i("AutoPPGInterval", "endHour: " + endHour );
        Log.i("AutoPPGInterval", "endMin: " + endMin );
        Log.i("AutoPPGInterval", "intervals: " + intervals );

        String durationValue = mDuration.getText().toString();
        if (TextUtils.isEmpty(durationValue)) {
            Toast.makeText(getActivity(), "can not be empty", Toast.LENGTH_SHORT).show();
            return false;
        }
        v = Integer.valueOf(durationValue);

        SPHelper.putPreference(getActivity(), KEY_AUTO_PPG_STARTHOUR, startHour);
        SPHelper.putPreference(getActivity(), KEY_AUTO_PPG_STARTMIN, startMin);
        SPHelper.putPreference(getActivity(), KEY_AUTO_PPG_ENDHOUR, endHour);
        SPHelper.putPreference(getActivity(), KEY_AUTO_PPG_ENDMIN, endMin);
        SPHelper.putPreference(getActivity(), KEY_AUTO_PPG_PPG, ppg);
        SPHelper.putPreference(getActivity(), KEY_AUTO_PPG_INTERVAL, intervals);
        SPHelper.putPreference(getActivity(), KEY_AUTO_PPG_DURATION, v);

        return true;
    }

    private final RadioGroup.OnCheckedChangeListener FormatCheckChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId == R.id.green){
                ppg = 0;
            }else if(checkedId == R.id.red){
                ppg = 1;
            }
/*            switch (checkedId){
                case R.id.green:
                    ppg = 0;
                    break;
                case R.id.red:
                    ppg = 1;
                    break;
                default:
                    break;
            }*/
        }
    };


    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }

    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };


    public interface ConfigCallBack {

        void onConfigOk();

        void onConfigCancel();
    }



}
